from flask import Blueprint, request, jsonify
from toutiao.model.db_routing import db
from toutiao.model.db_routing import Article, ArticleContent, \
    Attitude, Comment, CommentLiking

article_api = Blueprint('article', __name__, url_prefix='/article')


@article_api.route('/add', methods=['POST'])
# 文章添加接口
def add_article():
    """
    文章信息
    添加
    :return:
    """
    # 获取前端传入数据
    args = request.json
    user_id = args.get('user_id')
    channel_id = args.get('channel_id')
    title = args.get('title')
    # 判断数据是否齐全
    if not all([user_id, channel_id, title]):
        return jsonify({'code': 20001, 'message': '参数不全'})
    # 写入数据
    article = Article(user_id=user_id, channel_id=channel_id, title=title)
    db.session.add(article)
    db.session.commit()
    return jsonify({'code': 20000, 'message': '添加成功'})


@article_api.route('/delete', methods=['DELETE'])
# 文章删除接口
def delete_article():
    """
    文章接口
    删除
    :return:
    """
    # 获取文章id
    article_info = Article.query.get(id)
    # 判断数据是否存在
    if not article_info:
        # 返回数据
        return jsonify({'code': 400, 'msg': '该数据不存在'})
    # 删除数据
    Article.query.filter(Article.id == id).delete()
    # 执行删除
    db.session.commit()
    # 返回数据
    return jsonify({'code': 200, 'msg': '删除成功'})


@article_api.route('/update', methods=['PUT'])
def update_article():
    """
    文章接口
    修改
    :return:
    """
    # 获取前端信息
    args = request.json
    user_id = args.get('user_id')
    channel_id = args.get('channel_id')
    title = args.get('title')
    # id获取数据
    arti_info = Article.query.get(id)
    # 判断数据是否存在
    if not arti_info:
        return jsonify({'code': 400, 'msg': '该信息不存在'})
    # 修改对应字段
    if user_id and user_id != arti_info.user_id:
        arti_info.user_id = user_id
    if channel_id and channel_id != arti_info.channel_id:
        arti_info.channel_id = channel_id
    if title and title != arti_info.title:
        arti_info.title = title

    db.session.add(arti_info)
    return jsonify({'code': 200, 'msg': '修改成功'})


@article_api.route('/query', methods=['GET'])
def get_article():
    # 获取到所有数据
    arti_list = Article.query.all()
    # 建立一个空列表
    arti_all = []
    for article in arti_list:
        d = {
            'id': article.id,
            'user_id': article.user_id,
            'channel_id': article.channel_id,
            'title': article.title,
            'comment_count': article.comment_count,
        }
        arti_all.append(d)
    return jsonify({'code': 200, 'msg': '文章列表获取成功', 'data': arti_all})


@article_api.route('/sole', methods=['GET'])
def sole_article():
    arti_info = Article.query.git(id)

    if not arti_info:
        return jsonify({'code': 400, 'msg': '该信息不存在'})
    return jsonify({
        'code': 200,
        'msg': '获取成功',
        'data': {
            'id': arti_info.id,
            'user_id': arti_info.user_id,
            'channel_id': arti_info.channel_id,
            'title': arti_info.title,
            'comment_count': arti_info.comment_count,
        }
    })
